/*
 * fft_rj.cpp
 *
 *  Created on: 27.02.2013
 *      Author: Roma Jam
 */

#include "fft_rj.h"

fft_t Transform;

void fft_t::FFT(complex<double> *ArrIn, complex<double> *ArrOut, const int ArrSize, ffttype_t TypeFft){
    fftw_plan plan;
    // Choose type of FFT
    if(TypeFft == tf_backward) Type = FFTW_BACKWARD;
    else if(TypeFft == tf_forward) Type = FFTW_FORWARD;
//  Setup plan - which kind of FFT executed
    plan = fftw_plan_dft_1d(ArrSize, (fftw_complex*)ArrIn,(fftw_complex*)ArrOut, Type, FFTW_ESTIMATE);
    fftw_execute(plan);
    fftw_destroy_plan(plan);
    // Norm
//    Norm(ArrOut, ArrSize);
//    RestoreSpectrum(ArrIn, ArrOut, ArrSize);
}

int fft_t::MaxSearch(complex<double> *Arr, int aSize){
    complex<double> max_elmnt;
    int number;
    max_elmnt = 0;
    for (int i = 0; i < aSize; i++) {
        if(real(Arr[i]) > real(max_elmnt)) {
            real(max_elmnt) = real(Arr[i]);
            number = i;
        }
    }
    return number;
}

void fft_t::RestoreSpectrum(complex<double> *ArrIn, complex<double> *ArrOut, const int ASize){
    for (int i = 0; i < ASize; i++) {
        ArrOut[i] = (i < (ASize / 2))? ArrIn[(ASize / 2) + i] : ArrIn[i - (ASize / 2)];
    }
}

void fft_t::Norm(complex<double> *Arr, int aSize){
    for (int i = 0; i < aSize; ++i) {
        Arr[i] /= aSize;
    }
}

void fft_t::Reset(complex<double> *Arr, int aSize){
    for (int i = 0; i < aSize; ++i) {
        Arr[i] = 0;
    }
}
